package com.betterjr.modules.accountSupervision.service;

import com.betterjr.common.service.BaseService;
import com.betterjr.mapper.pagehelper.Page;
import com.betterjr.modules.accountSupervision.dao.ScfRepaymentMapper;
import com.betterjr.modules.accountSupervision.entity.ScfRepaymentBean;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 还款监控
 */
@Service
public class ScfRepaymentService extends BaseService<ScfRepaymentMapper, ScfRepaymentBean> {
//    private static final Logger logger = LoggerFactory.getLogger(ScfRepaymentService.class);

    /**
     *还款监控集合
     * @param anMap
     * @param flag  0为全部1为一周内放款数据 2 为逾期的
     * @param anPageNum
     * @param anPageSize
     * @return
     */
    public Page<ScfRepaymentBean> queryAllRepayment(Map<String, Object> anMap,
                                               final int flag, final int anPageNum, final int anPageSize) {
        if(anMap.size() < 4){
            return  new Page<>();
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        /**
         * flag = 1 时取一周内的放款数据
         */
        if(flag == 1){
            Calendar calendar = Calendar.getInstance();
            Date newDate = new Date(System.currentTimeMillis());
            calendar.setTime(newDate);
            calendar.add(Calendar.DAY_OF_YEAR, 7);
            Date t = calendar.getTime();
            String date = formatter.format(t);
            anMap.put("date1",date);
            anMap.put("date2",formatter.format(new Date()));
        }
        if(flag == 2){
            anMap.put("date1",formatter.format(new Date()));
        }
        Map<String,Object> map = this.mapper.getCount(anMap);
        Long count = (Long) map.get("num");
        int start = (anPageNum-1)*anPageSize;
        anMap.put("start",start);
        anMap.put("anPageSize",anPageSize);
        Page<ScfRepaymentBean> page = Page.listToPage(this.mapper.getList(anMap));
        page.setPageNum(anPageNum);
        page.setPageSize(anPageSize);
        page.setTotal(count);
        return page;
    }

    /**
     * 还款监控数量
     * @param flag 1为一周内放款数据 2 为逾期的
     * @return
     */
    public int queryCountRepayment(int flag,Map<String, Object> anMap){
        int count = 0;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        /**
         * flag = 1 时取一周内的放款数据
         */
        if(flag == 1){
            Calendar calendar = Calendar.getInstance();
            Date newDate = new Date(System.currentTimeMillis());
            calendar.setTime(newDate);
            calendar.add(Calendar.DAY_OF_YEAR, 7);
            Date t = calendar.getTime();
            String date = formatter.format(t);
            anMap.put("date1",date);
            anMap.put("date2",formatter.format(new Date()));
        }
        if(flag == 2){
            anMap.put("date1",formatter.format(new Date()));
        }
        Map<String,Object> map = this.mapper.getCount(anMap);
        count = ((Long)map.get("num")).intValue();
        return count;
    }
}
